package com.fjwt.gz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.entity.AgentEmployeeEntity;
import com.fjwt.gz.db.entity.FansAgentEntity;
import com.fjwt.gz.db.vo.AgentEmployeeVO;
import com.fjwt.gz.db.vo.FansAgentVO;
import com.fjwt.gz.service.mapper.FansAgentMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员绑定客户经理关系表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class FansAgentService extends ServiceImpl<FansAgentMapper, FansAgentEntity> {

    @Autowired
    protected AgentEmployeeService agentEmployeeService;
    @Autowired
    private FansAgentService fansAgentService;

    @Autowired
    private BlacklistService blacklistService;

    /**
     * 查询用户(客户经理)绑定机构
     * @param fansId
     */
    public AgentEmployeeVO getFansAgent(Long fansId,String appId) {
        FansAgentEntity fansAgentEntity = baseMapper.selectOne(FansAgentEntity.gw().eq(FansAgentEntity::getFansId, fansId).eq(FansAgentEntity::getAppId, appId).eq(FansAgentEntity::getIsDeleted, Constants.IS_DELETED.NO));
        if (ObjectUtils.isEmpty(fansAgentEntity)) {
            throw new BizException("未查询到客户经理所绑定机构");
        }
        AgentEmployeeEntity agentEmployeeEntity = agentEmployeeService.getById(fansAgentEntity.getAgentEmployeeId());
        if (ObjectUtils.isEmpty(agentEmployeeEntity)) {
            throw new BizException("未查询到客户经理所归属机构");
        }
        AgentEmployeeVO agentEmployeeVO = new AgentEmployeeVO();
        BeanUtils.copyProperties(agentEmployeeEntity, agentEmployeeVO);
        agentEmployeeVO.setFansId(fansAgentEntity.getFansId());
        return agentEmployeeVO;
    }

    /**
     * 根据会员id查询会员所绑定的信息
     * @param fansId
     * @return
     */
    public FansAgentVO selectEmployeeByFansId(Long fansId, String appId) {
        return baseMapper.selectEmployeeByFansId(fansId, appId);
    }
}
